function showChangePackageModal() {
    document.getElementById('changePackageModal').style.display = 'block';
}

function hideChangePackageModal() {
    document.getElementById('changePackageModal').style.display = 'none';
}

function toggleSubMenu() {
    var subMenu = document.getElementById('subMenu');
    if (subMenu.style.display === 'block') {
        subMenu.style.display = 'none';
    } else {
        subMenu.style.display = 'block';
    }
}

function checkBalanceBeforeChangePackage() {
    var balance = parseFloat('${user.balance}');
    var selectedPackage = document.querySelector('select[name="newPackage"]').value;
    var packagePrice;
    switch (selectedPackage) {
        case '1':
            packagePrice = 50;
            break;
        case '2':
            packagePrice = 60;
            break;
        case '3':
            packagePrice = 150;
            break;
        default:
            packagePrice = 0;
    }
    if (balance < packagePrice) {
        alert('余额不足，无法更改套餐，请先充值！');
        return false;
    }
    if (confirm('更改套餐将扣除'+ packagePrice +'元，确认更改吗？')) {
        return true;
    }
    return false;
}

function performAction(actionType, url, requiredSms, requiredBill, requiredTraffic) {
    // 获取用户当前资源
    var sms = parseInt('${user.sms}');
    var bill = parseFloat('${user.bill}');
    var traffic = parseFloat('${user.traffic}');
    var balance = parseFloat('${user.balance}');

    // 定义不同操作的资源补充规则
    var resourceRules = {
       'sendMessageToDad': {
            resourceType:'sms',
            required: requiredSms,
            cost: 1,
            amount: 10,
            message: '短信'
        },
       'sendLeaflets': {
            resourceType:'sms',
            required: requiredSms,
            cost: 1,
            amount: 10,
            message: '短信'
        },
        'callDad': {
            resourceType: 'bill',
            required: requiredBill,
            cost: 10,
            amount: 10,
            message: '通话'
        },
        'callMom': {
            resourceType: 'bill',
            required: requiredBill,
            cost: 20,
            amount: 20,
            message: '通话'
        },
        'playTeamfightTactics': {
            resourceType: 'traffic',
            required: requiredTraffic,
            cost: 1,
            amount: 1,
            message: '流量'
        },
        'playKingOfGlory': {
            resourceType: 'traffic',
            required: requiredTraffic,
            cost: 10,
            amount: 1,
            message: '流量'
        }
    };

    // 获取当前操作的规则
    var rule = resourceRules[actionType];
    if (!rule) {
        alert('未知操作类型');
        return false;
    }

    // 检查资源是否不足
    var resourceInsufficient = false;
    var userResource = 0;

    if (rule.resourceType ==='sms') {
        userResource = sms;
    } else if (rule.resourceType === 'bill') {
        userResource = bill;
    } else if (rule.resourceType === 'traffic') {
        userResource = traffic;
    }

    // 资源不足且余额足够时的提示
    var alertMessage = '';
    if (userResource < rule.required && balance >= rule.cost) {
        // 使用传统字符串拼接
        alertMessage = "您的" + rule.message + "已不足，即将花费" + rule.cost + "元购买";

        // 根据资源类型添加单位
        if (rule.message === '短信') {
            alertMessage += rule.amount + "条短信";
        } else if (rule.message === '通话') {
            alertMessage += rule.amount + "分钟通话";
        } else {
            alertMessage += rule.amount + "GB流量";
        }

        alert(alertMessage);
    }
    // 资源和余额都不足的提示
    else if (userResource < rule.required && balance < rule.cost) {
        alert("您的" + rule.message + "和余额均不足，无法购买额外" + rule.message);
        return false;
    }

    // 确认执行操作
    if (confirm('确认执行此操作吗？')) {
        window.location.href = url;
    }
    return true;
}

window.onload = function () {
	var rechargeSuccess = '<c:out value="${rechargeSuccess}"/>';
    var operationResult = '<c:out value="${operationResult}"/>';
    if (rechargeSuccess === 'true') {
        alert('充值成功');
    }
};